VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "WIN32 demo"
   ClientHeight    =   6900
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   6600
   BeginProperty Font 
      Name            =   "宋体"
      Size            =   9
      Charset         =   134
      Weight          =   700
      Underline       =   0   'False
      Italic          =   0   'False
      Strikethrough   =   0   'False
   EndProperty
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   ScaleHeight     =   6900
   ScaleWidth      =   6600
   StartUpPosition =   2  'CenterScreen
   WhatsThisHelp   =   -1  'True
   Begin VB.CheckBox Check1 
      Caption         =   "USB"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   495
      Left            =   360
      TabIndex        =   8
      Top             =   120
      Value           =   1  'Checked
      Width           =   1455
   End
   Begin VB.ComboBox Combo1 
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   360
      ItemData        =   "sample.frx":0000
      Left            =   4440
      List            =   "sample.frx":0002
      Style           =   2  'Dropdown List
      TabIndex        =   7
      Top             =   600
      Width           =   1695
   End
   Begin VB.CommandButton Command6 
      Caption         =   "Beep"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   420
      Left            =   360
      TabIndex        =   5
      Top             =   1320
      Width           =   1455
   End
   Begin VB.CommandButton Command4 
      Caption         =   "Exit"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   420
      Left            =   4680
      TabIndex        =   4
      Top             =   1320
      Width           =   1455
   End
   Begin VB.CommandButton Command3 
      Caption         =   "CardFunc"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   420
      Left            =   3240
      TabIndex        =   3
      Top             =   1320
      Width           =   1455
   End
   Begin VB.CommandButton Command2 
      Caption         =   "LoadKey"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   420
      Left            =   1800
      TabIndex        =   2
      Top             =   1320
      Width           =   1455
   End
   Begin VB.ListBox List1 
      BackColor       =   &H00FFFFFF&
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   4380
      Left            =   360
      TabIndex        =   1
      Top             =   2040
      Width           =   5895
   End
   Begin VB.CommandButton Command1 
      Caption         =   "Initial"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   420
      Left            =   360
      TabIndex        =   0
      Top             =   600
      Width           =   1455
   End
   Begin VB.Label Label1 
      Caption         =   "Chose device"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   2280
      TabIndex        =   6
      Top             =   600
      Width           =   1935
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Dim snr, tagtype As Long
Dim data32(16) As Byte
Dim databuff32(16) As Byte
Dim databuff32_hex(33) As Byte

Dim alldata As String * 3000
Dim rvalue As Long
Dim wvalue As Long
Dim cardmode As Integer
Dim loadmode As Integer
Dim sector As Integer
Dim displaydata(8) As Byte
Dim address As Integer
Dim cardtype As Integer 'CardType 0:M1,1:S70,2:Ultralight



Private Sub Combo1_Click()

Dim index As Integer

If icdev > 0 Then

index = Combo1.ListIndex
icdev = ihdevs(index)

End If

End Sub

Private Sub Combo2_Click()

cardtype = Combo2.ListIndex

If cardtype <> 2 Then

Command7.Enabled = True
Else
Command7.Enabled = False

End If


End Sub

Private Sub Command1_Click()

Dim tmphdev As Integer
Dim i As Integer
Dim strdev As String

Combo1.Clear

If Check1.value = 1 Then

i = 0

Do
  tmphdev = fw_init(100, 0)
  
  strdev = "设备"
  
  If tmphdev > 0 Then
  ihdevs(i) = tmphdev
  strdev = strdev + Str(i)
  
  Combo1.AddItem strdev
  Combo1.Text = strdev
  
  End If
  
  i = i + 1
  
Loop Until tmphdev < 0 And i < 100

icdev = ihdevs(0)


Else

'Serial Port
tmphdev = fw_init(0, 115200) 'COM1，Baud rate为115200

If tmphdev > 0 Then
Combo1.AddItem "Device 0"
Combo1.Text = "Device 0"
ihdevs(0) = tmphdev

End If

End If    'If Check1.value = 1 Then

icdev = ihdevs(0)

If icdev > 0 Then
Combo1.ListIndex = 0
End If

  

If icdev < 0 Then
  List1.AddItem ("Call fw_Load_key() function error!")
Else
List1.AddItem ("Call fw_Load_key() function success!")
End If

End Sub



Private Sub Command2_Click()

Dim hexkey(6) As Byte


hexkey(0) = &HFF 'A0
hexkey(1) = &HFF 'A1
hexkey(2) = &HFF 'A2
hexkey(3) = &HFF 'A3
hexkey(4) = &HFF 'A4
hexkey(5) = &HFF 'A5


st = fw_load_key(ByVal icdev, 0, 1, hexkey(0))   'load key of sector 1, to authentication then
If st <> 0 Then
      List1.AddItem ("Call fw_Load_key() function error!")
      Exit Sub
End If
List1.AddItem ("Call fw_Load_key() function success!")
End Sub

Private Sub Command3_Click()
Dim cardbuf(10) As Byte   'card serial number
Dim fASCorHEX As Byte     'what data type? ASCs or HEX string

cardmode = 1
address = 20
sector = 5

fASCorHEX = 1  'as type of ASCs
'fASCorHEX = 2 'as type of HEX string

Dim tagtype As Long
Dim LUID1 As Long
Dim LUID2 As Long
Dim Size1 As Byte
Dim lens As Integer
Dim strUID1 As String
Dim strUID2 As String
Dim UID As String
Dim i As Integer

st = fw_request(ByVal icdev, cardmode, tagtype)  ''when cardmode=1,can repeat to find a card,otherwise must take away the card before redo when 0
If st <> 0 Then
      List1.AddItem "Call fw_request() function error"
      Exit Sub
End If

st = fw_anticoll(ByVal icdev, 0, LUID1)
If st <> 0 Then
      List1.AddItem "Call fw_anticoll() function error"
      Exit Sub
End If

st = fw_select(ByVal icdev, ByVal LUID1, Size1)
If st <> 0 Then
      List1.AddItem "Call fw_select() function error"
      Exit Sub
End If

st = fw_anticoll2(ByVal icdev, 0, LUID2)
If st <> 0 Then
      List1.AddItem "Call fw_anticoll2() function error"
      Exit Sub
End If

st = fw_select2(ByVal icdev, ByVal LUID2)
If st <> 0 Then
      List1.AddItem "Call fw_select2() function error"
      Exit Sub
End If

strUID1 = Hex(LUID1)
lens = Len(strUID1)
If lens <> 8 Then
    For i = 0 To 8 - lens - 1
        strUID1 = "0" + strUID1
    Next i
End If

strUID2 = Hex(LUID2)
lens = Len(strUID2)
If lens <> 8 Then
    For i = 0 To 8 - lens - 1
        strUID2 = "0" + strUID2
    Next i
End If

UID = Mid(strUID1, 5, 2) + Mid(strUID1, 3, 2) + Mid(strUID1, 1, 2) + Mid(strUID2, 7, 2) + Mid(strUID2, 5, 2) + Mid(strUID2, 3, 2) + Mid(strUID2, 1, 2)

List1.AddItem UID  'show card serial number as HEX string

DoEvents


st = fw_authentication(ByVal icdev, 0, sector)  'Verify key
If st <> 0 Then
      List1.AddItem "Call fw_authentication() function error!"
      Exit Sub
End If
List1.AddItem "Call fw_authentication() function success!"
DoEvents

Dim a(17) As Byte

data32(0) = &H12
data32(1) = &H34
data32(2) = &H56
data32(3) = &H78
data32(4) = &H90
data32(5) = &HAB
data32(6) = &HCD
data32(7) = &HEF
data32(8) = &H11
data32(9) = &H22
data32(10) = &H33
data32(11) = &H44
data32(12) = &H55
data32(13) = &H66
data32(14) = &H77
data32(15) = &H88

If fASCorHEX = 1 Then 'write as ASC array type 'Write data to card
                                               'NOTE: case of M1,the 4th block is the key block,
                                               'you should not write the key block when testing
End If

If fASCorHEX = 2 Then

    Dim s As String
    s = "FONGWAH technology  "         'write string to a block
    Dim byt() As Byte
    byt = StrConv(s, vbFromUnicode) 'transform string to bytes

    st = fw_write(ByVal icdev, address, byt(0))
End If


If st <> 0 Then
      List1.AddItem "Call fw_write() function error!"
End If
List1.AddItem "Call fw_write() function success!"
DoEvents


st = fw_read(ByVal icdev, address, databuff32(0))   'Read card
If st <> 0 Then
     List1.AddItem "Call fw_read function error"
     Exit Sub

End If
List1.AddItem "Call fw_read function success"

Dim strRead As String

If fASCorHEX = 1 Then 'read as ASC array
    Call hex_a(databuff32_hex(0), databuff32(0), 32)
    strRead = StrConv(databuff32_hex, vbUnicode)
    
End If

 If fASCorHEX = 2 Then 'read as HEX array
    strRead = StrConv(databuff32, vbUnicode)
End If
    
List1.AddItem strRead

DoEvents

Dim akey(6) As Byte
Dim bkey(6) As Byte
Dim ctrbyts(4) As Byte

akey(0) = &HFF 'A0
akey(1) = &HFF 'A1
akey(2) = &HFF 'A2
akey(3) = &HFF 'A3
akey(4) = &HFF 'A4
akey(5) = &HFF 'A5

ctrbyts(0) = &HFF 'A0
ctrbyts(1) = &H7   'A1
ctrbyts(2) = &H80 'A2
ctrbyts(3) = &H69 'A3

bkey(0) = &HFF 'B0
bkey(1) = &HFF 'B1
bkey(2) = &HFF 'B2
bkey(3) = &HFF 'B3
bkey(4) = &HFF 'B4
bkey(5) = &HFF 'B5

'the key and control word is the same as original card,recommand not change the parameters 4，5，6
st = fw_changeb3(ByVal icdev, sector, akey(0), ctrbyts(0), 0, bkey(0))
If st <> 0 Then
    List1.AddItem "Call fw_changeb3() function error!"
    Exit Sub
End If
List1.AddItem "Call fw_changeb3() function success!"
DoEvents

'the following codes: operate a block as a value block
wvalue = 10000
st = fw_initval(ByVal icdev, address, ByVal wvalue) 'initial value
If st <> 0 Then
      List1.AddItem "Call fw_initval() function error"
      Exit Sub
End If
List1.AddItem "Call fw_initval() function success"
DoEvents


st = fw_increment(ByVal icdev, address, 888)    'add value
If st <> 0 Then
      List1.AddItem "Call fw_increment() function error"
      Exit Sub
End If
st = fw_transfer(ByVal icdev, address) 'you should call fw_transfer just after add value to valid
List1.AddItem "Call fw_increment() function success"
DoEvents

st = fw_decrement(ByVal icdev, address, 88)    'decrease value
If st <> 0 Then
      List1.AddItem "Call fw_decrement() function error"
      Exit Sub
End If
st = fw_transfer(ByVal icdev, address)     'you should call fw_transfer just after decrease value

List1.AddItem "Call fw_decrement() function success"
DoEvents

st = fw_readval(ByVal icdev, address, rvalue)    'Read value
If st <> 0 Then
      List1.AddItem "Call fw_readval() function error"
      Exit Sub
End If
List1.AddItem "Call fw_readval() function success"
List1.AddItem rvalue
DoEvents


st = fw_halt(ByVal icdev)                   'you should take away this card after next operation
If st <> 0 Then
      List1.AddItem "Call fw_halt() function error"
      Exit Sub
End If
List1.AddItem "Call fw_halt() function success"

DoEvents

List1.AddItem "Read/Write test passed!"

End Sub

Private Sub Command4_Click()
End
End Sub



Private Sub Command6_Click()


st = fw_beep(ByVal icdev, 10)
If st <> 0 Then
      List1.AddItem ("Call fw_beep() function error")
      Exit Sub
End If
List1.AddItem ("Call fw_beep() function success")
DoEvents

End Sub


Private Sub Form_Load()

Erase ihdevs
icdev = -1

End Sub

Private Sub Form_Unload(Cancel As Integer)
quit
End Sub

